import { getDetailsByIdApi } from "@/api/client/documentManagement/waybillManagement";
import { reactive, ref } from "vue";

export default function InitHook() {
  const formState = ref<Partial<ClientWaybillManagement.WaybillDetails>>({
    bizPreOrderShipmentFileResDtoList: []
  });
  const loading = ref<boolean>(false);
  const dialogOpts = reactive<Base.DialogOpts>({
    title: "修改送货仓库",
    width: "720px",
    type: 1,
    passData: {},
    component: "",
    visible: false
  });
  const drawerOpts = reactive<Base.DrawerOpts>({
    title: "修改送货仓库",
    width: "720px",
    type: 1,
    passData: {},
    component: "",
    visible: false
  });
  const map = new Map([
    [null, true],
    [1, true],
    [0, false]
  ]);
  const init = async (id: string) => {
    loading.value = true;
    return getDetailsByIdApi(id)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          formState.value = data;
          formState.value.customerPicimgShow = formState.value.customerPicimgShow === null ? true : formState.value.customerPicimgShow ? true : false;
          formState.value.bizPreOrderShipmentFileResDtoList = formState.value.bizPreOrderShipmentFileResDtoList?.map((e: any) => {
            return { ...e, customerShow: map.get(e.customerShow) };
          });
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  return {
    formState,
    loading,
    dialogOpts,
    init,
    drawerOpts
  };
}
